package vip.xiaonuo.smzq.modular.psba.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb02Kslx;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb03Kspj;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb06Kc;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb13Zycllx;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb02KslxMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb03KspjMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb06KcMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb13ZycllxMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.Kszlzb;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZlMapper;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtKjzycl;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtZl;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtZycl;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Cdfwnzycl;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaZycl;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbakczyclVo;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtZlMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtZyclMapper;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtZyclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 评审备案—资源储量 服务实现类
 *
 * @author sqq
 * @since 2024-05-22
 */
@Service
public class PsbaGtZyclServiceImpl extends ServiceImpl<PsbaGtZyclMapper, PsbaGtZycl> implements IPsbaGtZyclService {

    @Resource
    private PsbaGtZyclMapper psbaGtZyclMapper;
    @Resource
    private Cb06KcMapper cb06KcMapper; //矿产
    @Resource
    private Cb03KspjMapper cb03KspjMapper;//矿石品级
    @Resource
    private Cb02KslxMapper cb02KslxMapper;//矿石类型
    @Resource
    private PsbaGtZlMapper psbaGtZlMapper;//矿石质量
    @Resource
  private Cb13ZycllxMapper cb13ZycllxMapper;//资源储量类型

    @Override
    public Page<PsbaGtZycl> page(PsbaGtZycl psbaGtZycl) {
        QueryWrapper<PsbaGtZycl> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }


    public List<PsbakczyclVo> getPsbakczyclVoList2(String psbabh) {
        List<PsbakczyclVo> psbakczyclVoList = new ArrayList<>();
//        try{
            List<Integer> kcdmList = psbaGtZyclMapper.getKcdmList(psbabh);
            for (Integer kcdm : kcdmList){
                PsbakczyclVo psbakczyclVo = new PsbakczyclVo();
                QueryWrapper<PsbaGtZycl> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("kcdm",kcdm);
                queryWrapper.eq("psbabh",psbabh);
                List<PsbaGtZycl> psbaGtZyclList = psbaGtZyclMapper.selectList(queryWrapper);
                double jsbyl=0;
                double ksbyl=0;
                double ksljl=0;
                double jsljl=0;
                if (psbaGtZyclList!=null&&psbaGtZyclList.size()!=0){
                    for (PsbaGtZycl psbaGtZycl : psbaGtZyclList){
                        if (psbaGtZycl.getJsl()==null){
                            psbaGtZycl.setJsl(0.0);
                        }
                        if (psbaGtZycl.getKsl()==null){
                            psbaGtZycl.setKsl(0.0);
                        }
                        if (psbaGtZycl.getZycllb()==19001){
                            jsbyl=psbaGtZycl.getJsl();
                            ksbyl=psbaGtZycl.getKsl();
                        }
                        if (psbaGtZycl.getZycllb()==19002){
                            jsljl=psbaGtZycl.getJsl();
                            ksljl=psbaGtZycl.getKsl();
                        }
                    }
                    QueryWrapper<Cb13Zycllx> queryWrapperZycllx = new QueryWrapper<>();
                    queryWrapperZycllx.eq("dm",psbaGtZyclList.get(0).getZycllx());
                    Cb13Zycllx cb13Zycllx = cb13ZycllxMapper.selectOne(queryWrapperZycllx);
                    //ToDo 涉及新老数据库，需要修改同步
                    if (cb13Zycllx != null){
                        psbakczyclVo.setZycllxMc(cb13Zycllx.getMc());
                        psbakczyclVo.setZycllx(cb13Zycllx.getDm());
                    }else{
                        psbakczyclVo.setZycllxMc(psbaGtZyclList.get(0).getZycllx());
                        psbakczyclVo.setZycllx(psbaGtZyclList.get(0).getZycllx());
                    }
                    psbakczyclVo.setJslbul(jsbyl);
                    psbakczyclVo.setKslbul(ksbyl);
                    psbakczyclVo.setJslcml(jsljl);
                    psbakczyclVo.setKslcml(ksljl);

                    //矿石主要组分及质量指标
                    PsbaGtZycl psbaGtZycl=psbaGtZyclList.get(0);
                    QueryWrapper<PsbaGtZl> queryWrapperZl = new QueryWrapper<>();
                    queryWrapperZl.eq("kcdm",psbaGtZycl.getKcdm());
                    queryWrapperZl.eq("psbabh",psbaGtZycl.getPsbabh());
                    queryWrapperZl.eq("kslx",String.valueOf(psbaGtZycl.getKslx()));
                    queryWrapperZl.eq("kspj",String.valueOf(psbaGtZycl.getKspj()));
                    List<PsbaGtZl> psbaGtZlList = psbaGtZlMapper.selectList(queryWrapperZl);
                    if (psbaGtZlList!=null&&psbaGtZlList.size()!=0){
                        PsbaGtZl psbaGtZl = psbaGtZlList.get(0);
                        psbakczyclVo.setKszyzc(psbaGtZl.getZfm()+" "+psbaGtZl.getZfz()+" "+psbaGtZl.getZfdw());
                    }

                    //矿石工业类型及品牌
                    String kslxpj="";
                    QueryWrapper<Cb02Kslx> queryWrapperKslx=new QueryWrapper<>();
                    queryWrapperKslx.eq("kcdm",psbaGtZycl.getKcdm());
                    queryWrapperKslx.eq("kslx",psbaGtZycl.getKslx());
                    Cb02Kslx cb02Kslx=cb02KslxMapper.selectOne(queryWrapperKslx);
                    if (cb02Kslx!=null){
                        kslxpj=cb02Kslx.getKslxmc()+" ";
                    }

                    QueryWrapper<Cb03Kspj> queryWrapperKspj = new QueryWrapper<>();
                    queryWrapperKspj.eq("kcdm",psbaGtZycl.getKcdm());
                    queryWrapperKspj.eq("kspj",psbaGtZycl.getKspj());
                    Cb03Kspj cb03Kspj=cb03KspjMapper.selectOne(queryWrapperKspj);
                    if (cb03Kspj!=null){
                        kslxpj+=cb03Kspj.getKspjmc();
                    }
                    psbakczyclVo.setKsgylx(kslxpj);

                    //矿产名称
                    String kcmc="";
                    //统计对象
                    String tjdx="";
                    QueryWrapper<Cb06Kc> queryWrapperKc = new QueryWrapper<>();
                    queryWrapperKc.eq("kcdm",psbaGtZycl.getKcdm());
                    List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(queryWrapperKc);
                    if (cb06KcList!=null&&cb06KcList.size()!=0){
                        Cb06Kc cb06Kc=cb06KcList.get(0);
                        kcmc=cb06Kc.getKcmc()+"("+psbaGtZycl.getKczh()+")";
                        tjdx=cb06Kc.getKcmc();
                        if (cb06Kc.getJsldw()!=null&&!cb06Kc.getJsldw().equals("")){
                            tjdx=tjdx+" "+cb06Kc.getJsldw();
                        }else{
                            tjdx=tjdx+" "+cb06Kc.getKsldw();
                        }
                        psbakczyclVo.setKcmc(kcmc);
                        psbakczyclVo.setTjdx(tjdx);
                    }
                    psbakczyclVoList.add(psbakczyclVo);
                }
            }

//    }catch (Exception e){
//
//        }
        return psbakczyclVoList;
    }
    @Override
    public List<PsbakczyclVo> getPsbakczyclVoList(String psbabh) {
        List<PsbakczyclVo> psbakczyclVoList = new ArrayList<>();
//        try{
        List<String> kslxpjList =psbaGtZyclMapper.getKslxpjList(psbabh);
        if (kslxpjList != null && kslxpjList.size() > 0) {
            for (String kslxpj : kslxpjList) {
                // 使用逗号作为分隔符进行分割
                String[] parts = kslxpj.split(",");
                // 获取两个部分
                Integer kslx = Integer.valueOf(parts[0]);  // 逗号前的部分
                Integer kspj = Integer.valueOf(parts[1]);  // 逗号后（如果有的话）的部分
                Integer kcdm = Integer.valueOf(parts[2]);
                List<String> zycllzList = psbaGtZyclMapper.getZycllzList(psbabh, kslx, kspj,kcdm);
                if (zycllzList != null && zycllzList.size() > 0) {
                    for (String zycllz : zycllzList) {
                        PsbakczyclVo cdfwnzycl = new PsbakczyclVo();
                        QueryWrapper<Cb13Zycllx> queryWrapperZycllx = new QueryWrapper<>();
                        queryWrapperZycllx.eq("dm", zycllz);
                        Cb13Zycllx cb13Zycllx = cb13ZycllxMapper.selectOne(queryWrapperZycllx);
                        PsbaZycl psbaZycl = new PsbaZycl();
                        //ToDo 涉及新老数据库，需要修改同步
                        if (cb13Zycllx != null){
                            cdfwnzycl.setZycllxMc(cb13Zycllx.getMc());
                            cdfwnzycl.setZycllx(zycllz);
                        }else{
                            cdfwnzycl.setZycllxMc(zycllz);
                            cdfwnzycl.setZycllx(zycllz);
                        }
                        QueryWrapper<PsbaGtZycl> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("psbabh", psbabh);
                        queryWrapper.eq("kslx", kslx);
                        queryWrapper.eq("kspj", kspj);
                        queryWrapper.eq("kcdm", kcdm);
                        queryWrapper.eq("zycllx", zycllz);
                        List<PsbaGtZycl> psbaGtZyclList = psbaGtZyclMapper.selectList(queryWrapper);

                        if (psbaGtZyclList != null && psbaGtZyclList.size() > 0) {
                            PsbaGtZycl psbaGtZycl = psbaGtZyclList.get(0);

//                            cdfwnzycl.setZycllx(psbaGtZycl.getZycllx());

                            //矿石工业类型及品牌
                            String kslxpjmc = "";
                            QueryWrapper<Cb02Kslx> queryWrapperKslx = new QueryWrapper<>();
                            queryWrapperKslx.eq("kcdm", psbaGtZycl.getKcdm());
                            queryWrapperKslx.eq("kslx", psbaGtZycl.getKslx());
                            Cb02Kslx cb02Kslx = cb02KslxMapper.selectOne(queryWrapperKslx);
                            if (cb02Kslx != null) {
                                kslxpjmc = cb02Kslx.getKslxmc() + " ";
                            }

                            QueryWrapper<Cb03Kspj> queryWrapperKspj = new QueryWrapper<>();
                            queryWrapperKspj.eq("kcdm", psbaGtZycl.getKcdm());
                            queryWrapperKspj.eq("kspj", psbaGtZycl.getKspj());
                            Cb03Kspj cb03Kspj = cb03KspjMapper.selectOne(queryWrapperKspj);
                            if (cb03Kspj != null) {
                                kslxpjmc += cb03Kspj.getKspjmc();
                            }
                            cdfwnzycl.setKsgylx(kslxpjmc);


                            //矿石主要组分及质量指标
                            QueryWrapper<PsbaGtZl> queryWrapperZl = new QueryWrapper<>();
                            queryWrapperZl.eq("kcdm",psbaGtZycl.getKcdm());
                            queryWrapperZl.eq("psbabh",psbaGtZycl.getPsbabh());
                            queryWrapperZl.eq("kslx",String.valueOf(psbaGtZycl.getKslx()));
                            queryWrapperZl.eq("kspj",String.valueOf(psbaGtZycl.getKspj()));
                            List<PsbaGtZl> psbaGtZlList = psbaGtZlMapper.selectList(queryWrapperZl);
                            if (psbaGtZlList!=null&&psbaGtZlList.size()!=0){
                                PsbaGtZl psbaGtZl = psbaGtZlList.get(0);
                                cdfwnzycl.setKszyzc(psbaGtZl.getZfm()+" "+psbaGtZl.getZfz()+" "+psbaGtZl.getZfdw());
                            }

                            //矿产名称
                            String kcmc = "";
                            //统计对象
                            String tjdx = "";
                            QueryWrapper<Cb06Kc> queryWrapperKc = new QueryWrapper<>();
                            queryWrapperKc.eq("kcdm", psbaGtZycl.getKcdm());
                            List<Cb06Kc> cb06KcList = cb06KcMapper.selectList(queryWrapperKc);
                            if (cb06KcList != null && cb06KcList.size() != 0) {
                                Cb06Kc cb06Kc = cb06KcList.get(0);
                                kcmc = cb06Kc.getKcmc() + "(" + psbaGtZycl.getKczh() + ")";
                                tjdx = cb06Kc.getKcmc();
                                if (cb06Kc.getJsldw() != null && !cb06Kc.getJsldw().equals("")) {
                                    tjdx = tjdx + " " + cb06Kc.getJsldw();
                                } else {
                                    tjdx = tjdx + " " + cb06Kc.getKsldw();
                                }
                                cdfwnzycl.setKcmc(kcmc);
                                cdfwnzycl.setTjdx(tjdx);
                                for (PsbaGtZycl psbaGtZycl1 : psbaGtZyclList) {
                                    if (psbaGtZycl1.getZycllb() == 19001) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setJslbul(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setKslbul(psbaGtZycl1.getKsl());
                                    }
                                    if (psbaGtZycl1.getZycllb() == 19002) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setJslcml(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setKslcml(psbaGtZycl1.getKsl());
                                    }
                                    if (psbaGtZycl1.getZycllb() == 19006) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setKczjJsl(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setKczjKsl(psbaGtZycl1.getKsl());
                                    }
                                    if (psbaGtZycl1.getZycllb() == 19008) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setCszjJsl(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setCszjJsl(psbaGtZycl1.getKsl());
                                    }
                                    if (psbaGtZycl1.getZycllb() == 19009) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setSpyflJsl(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setSpyflKsl(psbaGtZycl1.getKsl());
                                    }
                                    if (psbaGtZycl1.getZycllb() == 19007) {
                                        if (psbaGtZycl1.getJsl() != null){
                                            cdfwnzycl.setOtherDataJsl(psbaGtZycl1.getJsl());
                                        }
                                        cdfwnzycl.setOtherDataKsl(psbaGtZycl1.getKsl());
                                    }
                                }
                                psbakczyclVoList.add(cdfwnzycl);
                            }
                        }
                    }
                }
            }
        }
//    }catch (Exception e){
//
//        }
        return psbakczyclVoList;
    }
}
